﻿using UnityEngine;
using UnityEngine.UI;
using ZCUIManager;

namespace ZCTouchDrawBoard
{
    public class PaintItemView : UIBaseView
    {
        #region Private UI
        private Image m_Image;
        private Button m_Button;
        private Image m_ImageHighLight;
        #endregion

        #region PrivateField
        private PaintAttribute m_PaintAttribute;
        #endregion

        #region PublicField
        public PaintAttribute PaintAttribute { 
            get
            {
                return m_PaintAttribute;
            }
            set
            {
                if (m_PaintAttribute == value)
                {
                    return;
                }
                m_PaintAttribute = value;
                Texture2D tex = Resources.Load<Texture2D>(m_PaintAttribute.IconPath);
                m_Image.sprite = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), new Vector2(0.5f, 0.5f));
                if (m_PaintAttribute.IsChangeColor)
                {
                    m_Image.color = m_PaintAttribute.PenColor;
                }
            }
        }
        public PaintPanel PaintPanel { get; set; }
        #endregion

        #region Override Method
        public override void Initialize(eUIType uiType, GameObject go)
        {
            base.Initialize(uiType, go);
            m_Image = Find(m_RectTransform, "Button").GetComponent<Image>();
            m_Button = Find(m_RectTransform, "Button").GetComponent<Button>();
            m_ImageHighLight = Find(m_RectTransform, "HighLight").GetComponent<Image>();
            HighLight(false);
        }

        public override void InitializeListener()
        {
            base.InitializeListener();
            m_Button.onClick.AddListener(OnClickListener);
        }
        #endregion

        #region Public Method
        public void OnClick()
        {
            m_Button.onClick.Invoke();
        }

        public void OnValueChange(int value)
        {
            PaintAttribute.PenSize = value;
            PaintPanel.SwitchPaint(PaintAttribute.PaintType, PaintAttribute);
        }

        public void HighLight(bool enable)
        {
            m_ImageHighLight.gameObject.SetActive(enable);
        }
        #endregion

        #region Listener
        private void OnClickListener()
        {
            PaintPanel.SelectPaintItemView = this;
            PaintPanel.SwitchPaint(PaintAttribute.PaintType, PaintAttribute);
        }
        #endregion

    }
}
